<%@page import="java.util.List,com.trw.trp.bean.ModelBean,com.trw.trp.bean.ModelDetail,com.trw.trp.bean.OrderBean,com.trw.trp.util.*"%>
<%@ include file="header.jsp"%>

<table align="center" cellpadding="0" cellspacing="0" height="100%"
	width="99.9%" border="0">
	<tr>
		<td valign="top">
		<div id="matrixDiv" />
		</td>
	</tr>
</table>
<style type="text/css">
.headerText {
	display: block;
	height: 150px;
	width: 150px;
	font-size: 13px;
	white-space: normal!important;
	vertical-align: bottom;
	-webkit-transform: rotate(-90deg);
	-moz-transform: rotate(-90deg);
	-o-transform: rotate(-90deg);
	filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);

}

.headerText2 {
	display: block;
	height: 130px;
	width: 130px;
	font-size: 13px;
	white-space: normal;
	vertical-align: bottom;
	-webkit-transform: rotate(-90deg);
	-moz-transform: rotate(-90deg);
	-o-transform: rotate(-90deg);
	filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}

.headerText3 {
	font-size: 13px;
	white-space: normal;
}

.x-grid3-hd-Column1 {
	margin-top: 100%;
	text-align: center;
}
</style>

<%step=2;
OrderBean order = (OrderBean)session.getAttribute(TRPConstants.CURRENT_ORDER);
UserBean user = (UserBean)session.getAttribute(TRPConstants.CURRENT_USER);
List<ModelBean> modelList = order.getModelMap().get(1);
%>
<script type="text/javascript">

function validateMatrix(){
	var result='',grid;
	<%for(int i=0;i<modelList.size();i++){
		ModelBean model = modelList.get(i);
		if(model.getModelId()==12){
	%>
	grid = Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>');
	if(grid){
		if(grid.getStore().getCount()==0){
			return 'Please upload file for Test Parts Information!';
		}
	}
	<%
		}
	}%>
	return result;
}

function saveMatrix(viewRequestFlag,action){
	/*if(action=='mail'||viewRequestFlag==true){
		var validateResult = validateMatrix();
		if(validateResult!=''){
			Ext.MessageBox.alert('Warning', validateResult);
			return;
		}
	}*/
	Ext.getBody().mask('saving data...');
	var grid,store,record;
	var params = new Object();
	<%for(int i=0;i<modelList.size();i++){
		ModelBean model = modelList.get(i);
	%>
	grid = Ext.getCmp('matrixGrid'+<%=i%>);
	if(grid){
		store = grid.getStore();
		for(var i=0;i<store.getCount();i++){
			record = store.getAt(i);
		<%if(model.getModelId()==12){%>
			eval('params.p_'+<%=i%>+'_'+i+'_166=record.data.idx;');
			eval('params.p_'+<%=i%>+'_'+i+'_168=record.data.comments;');
		<%}else if(model.getModelId()==20||model.getModelId()==21||model.getModelId()==22){%>
			
		<%}%>	
		}
	}
	<%}%>
	Ext.Ajax.request({
		url: 'saveMatrix.action',
		params: params,
		success: function(response){
			if(viewRequestFlag&&viewRequestFlag==true){
				document.location='viewOrder.action?orderId='+<%=order.getOrderId()%>;
			}else{
				flashMessage('data saved.','btnConfirm',1500);
				Ext.getBody().unmask();
				if(action=='mail'){
					sendMatrixMail();
				}else if(action=='download'){
					downloadMatrix();
				}
			}
		},
		failure: function(response){
			Ext.getBody().unmask();
			Ext.MessageBox.alert('Warning', 'Save data failed, please try again.');
		}
	});
}

function downloadMatrix(){
	Ext.getBody().mask('downloading...');
	Ext.Ajax.request({
		url: 'generateMatrix.action',
		params: {
			orderNumber: '<%=order.getOrderNumber()%>'
		},
		success: function(response){
			document.location='download.action?type=matrix'+'&orderNumber='+'<%=order.getOrderNumber()%>'+'&fileName=matrix.pdf';
			Ext.getBody().unmask();
		},
		failure: function(response){
			Ext.getBody().unmask();
			Ext.MessageBox.alert('Warning', 'download failed, please try again.');
		}
	});
}

function sendMatrixMail(){
	Ext.getBody().mask('Sending mail...');
	Ext.Ajax.request({
		url: 'sendEmail.action',
		params: {
			to: '<%=order.getGeneralInfo().getTestUser().getEmail()%>',
			cc: '<%=order.getGeneralInfo().getVvUser().getEmail()%>',
			emailId: 1
		},
		success: function(response){
			var grid;
			for(var idx=0;;idx++){
				grid = Ext.getCmp('matrixGrid'+idx);
				if(grid){
					grid.setDisabled(true);
					grid.getView().refresh();
				}else{
					break;
				}
			}
			Ext.getCmp('btnConfirm').setDisabled(true);
			Ext.getCmp('btnBack').setDisabled(true);
			Ext.getBody().unmask();
			var msg = Ext.util.JSON.decode(response.responseText);
			Ext.MessageBox.alert(msg.state==0?'Info':'Warning', msg.message);
		},
		failure: function(response){
			Ext.getBody().unmask();
			var msg = Ext.util.JSON.decode(response.responseText);
			Ext.MessageBox.alert('Warning', msg.message);
		}
	});
}

var matrixPnl;
Ext.onReady(function(){
	if(!matrixPnl){
		matrixPnl = Ext.create('Ext.panel.Panel',{
			forceFit: true,
			autoScroll:false,
			renderTo:'matrixDiv',
			items:[
		<%for(int i=0;i<modelList.size();i++){
			ModelBean model = modelList.get(i);
			if(model.getModelId()==12){
		%>
			{
				xtype:'fieldset',
				title:'<%=model.getModelName()%>',
				layout: {
					type: 'table',
					columns: 2,
					tableAttrs: {
						border:0,
						width:'100%'
					}
				},
				autoScroll:false,
				style: {
					marginLeft: 'auto',
					marginRight: 'auto'
				},
				items:[{
					xtype:'label',
					colspan:2,
					cls:'x-form-item',
					html:'You can re-order the test sequence from below matrix:'
				},{
					colspan:2,
					xtype:'grid',
					columnLines: true,
					id:'matrixGrid'+<%=i%>,
					forceFit: true,
					autoScroll:false,
					headerPosition:'top',
					enableColumnHide:false,
					enableColumnResize:false,
					sortableColumns:false,
					enableColumnMove:false,
					disabled:<%=order.getStatus()>99%>,
					multiSelect: true,
					viewConfig: {
						plugins: {
							ptype: 'gridviewdragdrop',
							dragText: 'move the records to the place you want'
						},
						listeners:{
							drop:function(){
								Ext.getCmp('matrixGrid'+<%=i%>).getView().refresh();
							}
						}
					},
					columns: [{
						text: '<span class="headerText3">Test No</span>',
						width:50,
						height:150,
						flex:1,
						dataIndex:'testNo',
						//style:{textAlign:'left'},
						renderer: function(value,a,obj,idx){
							return idx+1;
						}	
					},{
						text: '<span class="headerText3">Test Temp</span>',
						width:50,
						flex:1,
						dataIndex:'testTemp'
					},{
						text: '<span class="headerText3">Inflator Type</span>',
						width:60,
						flex:1,
						dataIndex:'inflatorType'
					},{
						text: '<span class="headerText3">Side</span>',
						width:50,
						flex:1,
						dataIndex:'side'
					},{
						header: '<span class="headerText3">Ignition Current: (A)</span>',
						width:70,
						dataIndex: 'ignitionC'
					},{
						header: '<span class="headerText3">Ignition Current Duration: (ms)</span>',
						width:70,
						dataIndex: 'ignitionCD'
					},{
						header: '<span class="headerText3">Ignition Resistance: (&Omega;)</span>',
						width:70,
						dataIndex: 'ignitionR'
					},{
						header: '<span class="headerText3">Time from removal to ignition: (s)</span>',
						width:70,
						dataIndex: 'timeFRTI'
					},{
						header: '<span class="headerText3">Storage Temperature (&#8451;)</span>',
						width:70,
						dataIndex: 'storageTemp'
					},{
						header: '<span class="headerText3">Storage Time(h)</span>',
						columns:[{
							header: '<span class="headerText3">Min</span>',
							width:35,
							dataIndex: 'min'
						},{
							header: '<span class="headerText3">Max</span>',
							width:35,
							dataIndex: 'max'
						}]
					},{
						header: '<span class="headerText3">Restorage Time (min)</span>',
						width:70,
						dataIndex: 'restorageTime'
					},{
						header: '<span class="headerText3">Module mounting time (s)</span>',
						width:70,
						dataIndex: 'moduleMountingTime'
					},{
						header: '<span class="headerText3">Test Temperature (&#8451;)</span>',
						width:70,
						dataIndex: 'testTemperature'
					},{
						header: '<span class="headerText3">Comments</span>',
						flex:1,
						minWidth:65
					}],
					store: Ext.create('Ext.data.Store', {
						autoLoad:true,
						fields: [{name:'idx'},{name:'testNo'},{name:'testTemp'},{name:'side'},{name:'inflatorType'},
								 {name:'ignitionC'},{name:'ignitionCD'},{name:'ignitionR'},{name:'timeFRTI'},{name:'storageTemp'},
								 {name:'tolerance1'},{name:'min'},{name:'max'},{name:'restorageTime'},{name:'moduleMountingTime'},{name:'testTemperature'},
								 {name:'tolerance2'},{name:'comments'}],
						proxy: {
							type: 'ajax',
							url: 'getMatrix.action?idx=<%=i%>',
							reader: {
								type: 'json',
								root: 'items',
								totalProperty: 'size'
							}
						}
					})
				},{
					xtype:'label',
					colspan:2,
					cls:'x-form-item',
					html:'<br><br>Spare Parts:'
				},{
					colspan:2,
					xtype:'grid',
					columnLines: true,
					forceFit: true,
					autoScroll:false,
					headerPosition:'top',
					enableColumnHide:false,
					enableColumnResize:false,
					sortableColumns:false,
					enableColumnMove:false,
					columns: [{
						text: '<span class="headerText3">Test No</span>',
						width:50,
						height:150,
						flex:1,
						dataIndex:'testNo',
						renderer: function(value,a,obj,idx){
							return 'SP'+(idx+1);
						}	
					},{
						text: '<span class="headerText3">Test Temp</span>',
						width:50,
						flex:1,
						dataIndex:'testTemp'
					},{
						text: '<span class="headerText3">Inflator Type</span>',
						width:60,
						flex:1,
						dataIndex:'inflatorType'
					},{
						text: '<span class="headerText3">Side</span>',
						width:50,
						flex:1,
						dataIndex:'side'
					},{
						header: '<span class="headerText3">Ignition Current: (A)</span>',
						width:70,
						dataIndex: 'ignitionC'
					},{
						header: '<span class="headerText3">Ignition Current Duration: (ms)</span>',
						width:70,
						dataIndex: 'ignitionCD'
					},{
						header: '<span class="headerText3">Ignition Resistance: (&Omega;)</span>',
						width:70,
						dataIndex: 'ignitionR'
					},{
						header: '<span class="headerText3">Time from removal to ignition: (s)</span>',
						width:70,
						dataIndex: 'timeFRTI'
					},{
						header: '<span class="headerText3">Storage Temperature (&#8451;)</span>',
						width:70,
						dataIndex: 'storageTemp'
					},{
						header: '<span class="headerText3">Storage Time(h)</span>',
						columns:[{
							header: '<span class="headerText3">Min</span>',
							width:35,
							dataIndex: 'min'
						},{
							header: '<span class="headerText3">Max</span>',
							width:35,
							dataIndex: 'max'
						}]
					},{
						header: '<span class="headerText3">Restorage Time (min)</span>',
						width:70,
						dataIndex: 'restorageTime'
					},{
						header: '<span class="headerText3">Module mounting time (s)</span>',
						width:70,
						dataIndex: 'moduleMountingTime'
					},{
						header: '<span class="headerText3">Test Temperature (&#8451;)</span>',
						width:70,
						dataIndex: 'testTemperature'
					}],
					store: Ext.create('Ext.data.Store', {
						autoLoad:true,
						fields: [{name:'idx'},{name:'testNo'},{name:'testTemp'},{name:'side'},{name:'inflatorType'},
								 {name:'ignitionC'},{name:'ignitionCD'},{name:'ignitionR'},{name:'timeFRTI'},{name:'storageTemp'},
								 {name:'min'},{name:'max'},{name:'restorageTime'},{name:'moduleMountingTime'},{name:'testTemperature'}],
						proxy: {
							type: 'ajax',
							url: 'getSpareParts.action?idx=<%=i%>',
							reader: {
								type: 'json',
								root: 'items',
								totalProperty: 'size'
							}
						}
					})
				}]
			}
		<%}else if(model.getModelId()==20||model.getModelId()==21||model.getModelId()==22||model.getModelId()==23){%>
			{
				xtype:'fieldset',
				title:'<%=model.getModelName()%>',
				autoScroll:false,
				style: {
					marginLeft: 'auto',
					marginRight: 'auto'
				},
				items:[{
					xtype:'grid',
					id:'matrixGrid'+'<%=i%>',
					disabled:<%=order.getStatus()>99%>,
					title: 'Matrix Information',
					loadMask: true,
					forceFit:true,
					autoScroll:true,
					enableColumnHide:false,
					sortableColumns:false,
					columnLines:true,
					autoHeight:true,
					columns: [
						{xtype: 'rownumberer',header:'No.',width: 40},
						{header: '<span class="headerText3">Ignition Current (A)</span>',dataIndex: 'ignitionC',width:50},
						{header: '<span class="headerText3">Current Duration (ms)</span>',dataIndex: 'currentDur',width:50},
						{header: '<span class="headerText3">Frame Per Second</span>',dataIndex: 'framePerSec',width:50},
						{header: '<span class="headerText3">Video Record Time(ms)</span>',dataIndex: 'videoRecTime',width:50},
						{header: '<span class="headerText3">Temperature(&#8451;)</span>',dataIndex: 'temp',width:50},
						{header: '<span class="headerText3">Storage time(h)</span>',dataIndex: 'storageTime',width:50},
						{header: '<span class="headerText3">Dual Stage inflator</span>',dataIndex: 'dualStageInflator',width:50},
						{header: '<span class="headerText3">Stage Delay(ms)</span>',dataIndex: 'stageD',width:50},
					<%if(model.getModelId()==20||model.getModelId()==23){%>
						{header: 'Impact Dimension',
							columns:[{
								header: '<span class="headerText3">Length (mm)</span>',
								width:50,
								dataIndex: 'length'
							},{
								header: '<span class="headerText3">Width (mm)</span>',
								width:50,
								dataIndex: 'width'
							}]
						},
					<%}%>
					<%if(model.getModelId()==21){%>
						{header: '<span class="headerText3">Impact Type</span>',dataIndex: 'impactType',width:50},
					<%}else{%>
						{header: '<span class="headerText3">Impact Mass(kg)</span>',dataIndex: 'impactMass',width:50},
					<%}%>
					<%if(model.getModelId()==22){%>
						{header: '<span class="headerText3">Column Angle(&deg;)</span>',dataIndex: 'columnAngle',width:50},
					<%}%>
					<%if(model.getModelId()==23){%>
						{header: '<span class="headerText3">Push Distance</span>',dataIndex: 'PushDistance',width:50},
					<%}else{%>
						{header: '<span class="headerText3">Velocity</span>',dataIndex: 'Velocity',width:50},
					<%}%>
						{header: '<span class="headerText3">Test Quantity</span>',dataIndex: 'TestQuantity',width:50},
						{header: '<span class="headerText3">Position</span>',dataIndex: 'Position',width:50},
						{header: '<span class="headerText3">Temperature</span>',dataIndex: 'Temperature',width:50},
						{header: '<span class="headerText3">Module Parameter</span>',dataIndex: 'ModuleParameter',width:50}
					],
					store: Ext.create('Ext.data.Store', {
						autoLoad:true,
						fields: [{name:'ignitionC'},{name:'currentDur'},{name:'framePerSec'},{name:'videoRecTime'},{name:'temp'},{name:'storageTime'},
								 {name:'dualStageInflator'},{name:'stageD'},{name:'impactType'},{name:'length'},{name:'width'},{name:'impactMass'},{name:'columnAngle'},
								 {name:'Velocity'},{name:'PushDistance'},{name:'TestQuantity'},{name:'Position'},{name:'Temperature'},{name:'ModuleParameter'}],
						proxy: {
							type: 'ajax',
							url: 'getMatrix.action?idx=<%=i%>',
							reader: {
								type: 'json',
								root: 'items',
								totalProperty: 'size'
							}
						}
					})
				}]
			}
		<%}else{%>
			{
				xtype:'fieldset',
				title:'<%=model.getModelName()%>',
				autoScroll:false,
				style: {
					marginLeft: 'auto',
					marginRight: 'auto'
				},
				items:[{
					xtype:'label',
					cls:'x-form-item',
					text:'Matrix not support for this model.'	
				}]
			}
		<%}if(i<modelList.size()-1){out.print(",");}
		}%>	
			],
			buttonAlign:'center',
			buttons:[
			<%if(order.getUser().getUserId()==user.getUserId()){%>
			{
				text:'Back',
				id:'btnBack',
				cls:'big-button',
				disabled:<%=order.getStatus()>99%>,
				handler:function (){
					document.location='goSubModel.action';
				}
			},{
				text:'Send Email',
				cls:'big-button',
				handler:function(){
					if(Ext.getCmp('btnBack')&&Ext.getCmp('btnBack').isDisabled()){
						sendMatrixMail();
					}else{
						Ext.MessageBox.confirm('Confirm', 'The test sequence can\'t be re-ordered after send mail?Are you sure you want to do that?', function(btn){
							if(btn=='yes'){
								saveMatrix(false,'mail');
							}
						});
					}
				}
			},
			<%}%>
			{
				text:'Download',
				cls:'big-button',
				handler:function(){
					if(Ext.getCmp('btnBack')&&Ext.getCmp('btnBack').isDisabled()){
						downloadMatrix();
					}else{
						saveMatrix(false,'download');
					}
				}
			},
			<%if(order.getUser().getUserId()==user.getUserId()){%>
			{
				text:'Save and Confirm',
				cls:'big-button',
				id:'btnConfirm',
				disabled:<%=order.getStatus()>99%>,
				handler:function(){
					Ext.MessageBox.confirm('Confirm', 'Please confirm the test sequence, it can\'t be re-ordered after send mail?Are you sure you want to do that?', function(btn){
						if(btn=='yes'){
							saveMatrix(true);
						}
					});
				}
			},
			<%}%>
			{
				xtype:'hidden'
			}]
		});
	}
});
</script>
<%@ include file="footer.jsp"%>